/////////////////////////////////////////////////////////////////////////////////////////////
// Filename: MessageDispatcher.cpp
// (c) Dave Ottley, 2012. All rights reserved.
/////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////
//		INCLUDES
///////////////////////////


///////////////////////////
//		MY INCLUDES
///////////////////////////
#include "MessageDispatcher.h"
#include "EntityManager.h"
#include "BaseGameEntity.h"
#include "Telegram.h"

MessageDispatcher * MessageDispatcher::GetInstance()
{
	static MessageDispatcher theOne;

	return &theOne;
}

bool MessageDispatcher::Discharge(BaseGameEntity * receiver, const Telegram& telegram)
{
	if( !receiver->HandleMessage(telegram))
	{
		//telegram could not be handled
		#ifdef SHOW_MESSAGING_INFO
		debug_con << L"Message not handled" << L" ";
		#endif
		return false;
	}

	return true;
}

bool MessageDispatcher::DispatchMessage(double	 delay,
										uint32_t sender,
										uint32_t receiver,
										uint32_t msg,
										void *	 extraInfo = NULL)
{
	BaseGameEntity* pReceiver = EntityMgr->GetEntityFromID(receiver);

	if(receiver == NULL)
	{
		#ifdef SHOW_MESSAGING_INFO
		debug_con << "\nWarning! No Receiver with ID of " << receiver << " found" << "";
		#endif

		return false;
	}

	Telegram telegram(0.0, sender, receiver, msg, extraInfo);

	#ifdef SHOW_MESSAGING_INFO
    debug_con << "\nTelegram dispatched at time: " << TickCounter->GetCurrentFrame()
         << " by " << sender << " for " << receiver 
         << ". Msg is " << msg << "";
    #endif

	return Discharge(pReceiver, telegram);
}